package com.hsq.ndmall.promotion.listening;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hsq.ndmall.common.utils.CouponUserConstant;
import com.hsq.ndmall.common.utils.R;
import com.hsq.ndmall.mq.constant.OrderRabbitMQ;
import com.hsq.ndmall.mq.dto.OrderMsgDto;
import com.hsq.ndmall.promotion.entity.CouponUserEntity;
import com.hsq.ndmall.promotion.feign.OrderFeign;
import com.hsq.ndmall.promotion.service.CouponUserService;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.A;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 黄四清
 * @version 1.0.0
 * @ClassName ConsumerRabbit.java
 * @Description TODO  监听订单创建之后的,需要将优惠券状态设为未已使用
 * @createTime 2021年09月03日 17:35:00
 */
@RabbitListener(queues = {OrderRabbitMQ.ORDER_RELEASE_COUPON_QUEUE})
@Component
@Slf4j
public class ConsumerRabbit {

    @Autowired
    OrderFeign orderFeign;

    @Autowired
    CouponUserService couponUserService;

    @RabbitHandler
    public void receivesMessage(OrderMsgDto orderDto) {
        log.info("我消费了：{}", orderDto.getOrderId());
        // TODO 1、查询订单状态，如果订单不存在，就需要将使用的优惠券设计为0（未使用）
        // 根据订单ID查询优惠券
        QueryWrapper<CouponUserEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("order_id", orderDto.getOrderId());
        List<CouponUserEntity> list = couponUserService.list(wrapper);
        if (list != null && list.size() != 0) {
            CouponUserEntity couponUserEntity = list.get(0);
            System.out.println(couponUserEntity);
            // 修改优惠券的使用状态
            couponUserEntity.setStatus(0);
            couponUserEntity.setOrderId(null);
            couponUserService.updateById(couponUserEntity);
            log.info("修改成功！");
        }
    }
}
